package com.gisocn.onemap.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.UserHandle;
import android.os.storage.StorageManager;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import com.gisocn.onemap.model.LineStringObject;
import com.gisocn.onemap.model.MultLinesStringObject;
import com.gisocn.onemap.model.MultiIPointObject;
import com.gisocn.onemap.model.PointObject;
import com.gisocn.onemap.model.Polygon;
import com.google.gson.Gson;
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import org.codehaus.jackson.util.MinimalPrettyPrinter;

/* loaded from: classes2.dex */
public class Utils {
    public static String getMULTIPOLYGONWktToJson(String str, int i) {
        Polygon polygon = new Polygon();
        ArrayList arrayList = new ArrayList();
        String[] split = str.substring(0, str.length() - 1).split("\\(", 2);
        String[] split2 = split[1].substring(0, split[1].length() - 1).split("\\(", 2)[1].split("\\), \\(");
        if (split2.length == 1) {
            for (String str2 : split2) {
                String trim = str2.trim();
                String[] split3 = trim.substring(1, trim.length() - 1).split(",");
                ArrayList arrayList2 = new ArrayList();
                for (String str3 : split3) {
                    String[] split4 = str3.trim().split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
                    arrayList2.add(new Double[]{Double.valueOf(Double.parseDouble(split4[0])), Double.valueOf(Double.parseDouble(split4[1]))});
                }
                arrayList.add(arrayList2);
            }
        } else {
            for (String str4 : split2) {
                String trim2 = str4.trim();
                String[] split5 = trim2.substring(1, trim2.length() - 1).split(",");
                ArrayList arrayList3 = new ArrayList();
                for (int i2 = 1; i2 < split5.length; i2++) {
                    String[] split6 = split5[i2].trim().split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
                    arrayList3.add(new Double[]{Double.valueOf(Double.parseDouble(split6[0])), Double.valueOf(Double.parseDouble(split6[1]))});
                }
                arrayList.add(arrayList3);
            }
        }
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("wkid", Integer.valueOf(i));
        polygon.setRings(arrayList);
        polygon.setSpatialReference(hashMap);
        return new Gson().toJson(polygon);
    }

    public static String getPOINTWktToJson(String str, int i) {
        String[] split = str.split("\\(");
        String[] split2 = split[1].substring(0, split[1].length() - 1).split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
        PointObject pointObject = new PointObject();
        pointObject.setX(Double.parseDouble(split2[0]));
        pointObject.setY(Double.parseDouble(split2[1]));
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("wkid", Integer.valueOf(i));
        pointObject.setSpatialReference(hashMap);
        return new Gson().toJson(pointObject);
    }

    public String getMULTIPOINTWktToJson(String str, int i) {
        MultiIPointObject multiIPointObject = new MultiIPointObject();
        String[] split = str.substring(0, str.length() - 1).split("\\(\\(");
        String[] split2 = split[1].substring(0, split[1].length() - 1).split(",");
        ArrayList arrayList = new ArrayList();
        for (int i2 = 0; i2 < split2.length; i2++) {
            if (i2 == 0) {
                String[] split3 = split2[i2].substring(0, split2[i2].length() - 1).split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
                arrayList.add(new Double[]{Double.valueOf(Double.parseDouble(split3[0])), Double.valueOf(Double.parseDouble(split3[1]))});
            } else if (i2 == split2.length) {
                String[] split4 = split2[i2].substring(1, split2[i2].length()).split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
                arrayList.add(new Double[]{Double.valueOf(Double.parseDouble(split4[0])), Double.valueOf(Double.parseDouble(split4[1]))});
            } else {
                String trim = split2[i2].trim();
                String[] split5 = trim.substring(1, trim.length() - 1).split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
                arrayList.add(new Double[]{Double.valueOf(Double.parseDouble(split5[0])), Double.valueOf(Double.parseDouble(split5[1]))});
            }
        }
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("wkid", Integer.valueOf(i));
        multiIPointObject.setPoints(arrayList);
        multiIPointObject.setSpatialReference(hashMap);
        return new Gson().toJson(multiIPointObject);
    }

    public static String getLINESTRINGWktToJson(String str, int i) {
        LineStringObject lineStringObject = new LineStringObject();
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        String[] split = str.split("\\(");
        for (String str2 : split[1].substring(0, split[1].length() - 1).split(",")) {
            String[] split2 = str2.trim().split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
            arrayList2.add(new Double[]{Double.valueOf(Double.parseDouble(split2[0])), Double.valueOf(Double.parseDouble(split2[1]))});
        }
        arrayList.add(arrayList2);
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("wkid", Integer.valueOf(i));
        lineStringObject.setPaths(arrayList);
        lineStringObject.setSpatialReference(hashMap);
        return new Gson().toJson(lineStringObject);
    }

    public static String getMULTILINESTRINGWktToJson(String str, int i) {
        MultLinesStringObject multLinesStringObject = new MultLinesStringObject();
        ArrayList arrayList = new ArrayList();
        for (String str2 : str.substring(0, str.length() - 1).split("\\(", 2)[1].split("\\),\\(")) {
            String trim = str2.trim();
            String[] split = trim.substring(1, trim.length() - 1).split(",");
            ArrayList arrayList2 = new ArrayList();
            for (String str3 : split) {
                String[] split2 = str3.trim().split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
                arrayList2.add(new Double[]{Double.valueOf(Double.parseDouble(split2[0])), Double.valueOf(Double.parseDouble(split2[1]))});
            }
            arrayList.add(arrayList2);
        }
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("wkid", Integer.valueOf(i));
        multLinesStringObject.setRings(arrayList);
        multLinesStringObject.setSpatialReference(hashMap);
        return new Gson().toJson(multLinesStringObject);
    }

    public static String getPOLYGONWktToJson(String str, int i) {
        Polygon polygon = new Polygon();
        ArrayList arrayList = new ArrayList();
        for (String str2 : str.substring(0, str.length() - 1).split("\\(", 2)[1].split("\\), \\(")) {
            String trim = str2.trim();
            String[] split = trim.substring(1, trim.length() - 1).split(",");
            ArrayList arrayList2 = new ArrayList();
            for (String str3 : split) {
                String[] split2 = str3.trim().split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
                arrayList2.add(new Double[]{Double.valueOf(Double.parseDouble(split2[0])), Double.valueOf(Double.parseDouble(split2[1]))});
            }
            arrayList.add(arrayList2);
        }
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("wkid", Integer.valueOf(i));
        polygon.setRings(arrayList);
        polygon.setSpatialReference(hashMap);
        return new Gson().toJson(polygon);
    }

    public static String getAngle(float f) {
        if (f < 0.0f) {
            f += 360.0f;
        }
        float f2 = (int) f;
        if (0.0f < f2 && f2 < 45.0f) {
            return "北偏东" + f2 + "度";
        } else if (45.0f < f2 && f2 < 90.0f) {
            return "东偏北" + (90.0f - f2) + "度";
        } else if (90.0f < f2 && f2 < 135.0f) {
            return "东偏南" + (f2 - 90.0f) + "度";
        } else if (135.0f < f2 && f2 < 180.0f) {
            return "南偏东" + (180.0f - f2) + "度";
        } else if (180.0f < f2 && f2 < 225.0f) {
            return "南偏西" + (f2 - 180.0f) + "度";
        } else if (225.0f < f2 && f2 < 270.0f) {
            return "西偏南" + (270.0f - f2) + "度";
        } else if (270.0f < f2 && f2 < 315.0f) {
            return "西偏北" + (f2 - 270.0f) + "度";
        } else if (315.0f >= f2 || f2 >= 360.0f) {
            return (f2 == 0.0f || f2 == 360.0f) ? "正北" : f2 == 45.0f ? "正东北" : f2 == 90.0f ? "正东" : f2 == 135.0f ? "正东南" : f2 == 180.0f ? "正南" : f2 == 225.0f ? "正西南" : f2 == 270.0f ? "正西" : f2 == 315.0f ? "正西北" : "";
        } else {
            return "北偏西" + (360.0f - f2) + "度";
        }
    }

    public static boolean isNumeric(String str) {
        return Boolean.valueOf(str.matches("-?[0-9]+.*[0-9]*")).booleanValue();
    }

    public static Uri getImageContentUri(Context context, File file) {
        String absolutePath = file.getAbsolutePath();
        Cursor query = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_id"}, "_data=? ", new String[]{absolutePath}, null);
        if (query != null && query.moveToFirst()) {
            int i = query.getInt(query.getColumnIndex("_id"));
            Uri parse = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(parse, "" + i);
        } else if (file.exists()) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("_data", absolutePath);
            return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        } else {
            return null;
        }
    }

    public static File uriToFile(Uri uri, Context context) {
        int i = 0;
        if ("file".equals(uri.getScheme())) {
            String encodedPath = uri.getEncodedPath();
            if (encodedPath != null) {
                encodedPath = Uri.decode(encodedPath);
                ContentResolver contentResolver = context.getContentResolver();
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("(");
                stringBuffer.append("_data");
                stringBuffer.append("=");
                stringBuffer.append("'" + encodedPath + "'");
                stringBuffer.append(")");
                Cursor query = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "_data"}, stringBuffer.toString(), null, null);
                query.moveToFirst();
                while (!query.isAfterLast()) {
                    i = query.getInt(query.getColumnIndex("_id"));
                    encodedPath = query.getString(query.getColumnIndex("_data"));
                    query.moveToNext();
                }
                query.close();
                if (i != 0) {
                    Uri parse = Uri.parse("content://media/external/images/media/" + i);
                    PrintStream printStream = System.out;
                    printStream.println("temp uri is :" + parse);
                }
            }
            if (encodedPath != null) {
                return new File(encodedPath);
            }
        } else if ("content".equals(uri.getScheme())) {
            Cursor query2 = context.getContentResolver().query(uri, new String[]{"_data"}, null, null, null);
            String string = query2.moveToFirst() ? query2.getString(query2.getColumnIndexOrThrow("_data")) : null;
            query2.close();
            return new File(string);
        }
        return null;
    }

    public static Bitmap viewConversionBitmap(View view, View view2) {
        int width = view2.getWidth();
        int height = view2.getHeight();
        L.e("viewConversionBitmap:" + width);
        L.e("viewConversionBitmap:" + height);
        L.e("viewConversionBitmap:" + view2.getWidth());
        L.e("viewConversionBitmap:" + view2.getHeight());
        Bitmap createBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        canvas.drawColor(0);
        view.layout(0, 0, width, height);
        view.draw(canvas);
        return createBitmap;
    }

    public static Bitmap viewConversionBitmap(View view) {
        int width = view.getWidth();
        int height = view.getHeight();
        Bitmap createBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        canvas.drawColor(-1);
        view.layout(0, 0, width, height);
        view.draw(canvas);
        return createBitmap;
    }

    public static String stampToDate(String str) {
        if (!TextUtils.isEmpty(str) && !"null".equals(str)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (str.length() == 13) {
                str = simpleDateFormat.format(new Date(Long.parseLong(str)));
            } else {
                str = simpleDateFormat.format(new Date(Integer.parseInt(str) * 1000));
            }
            L.e("转化的日期" + str);
        }
        return str;
    }

    public static String getBaseMapName(String str, String str2) {
        if (str.equals("底图") || str.equals("矢量图") || str.equals("地图")) {
            str2 = str2.contains("注记") ? "CVA_C" : "VEC_C";
        } else if (str2.contains("注记")) {
            str2 = "CIA_C";
        }
        L.e("getBaseMapName" + str2);
        return str2;
    }

    public static String getSdPath(Context context) {
        String str;
        if (Build.VERSION.SDK_INT >= 23) {
            if (Build.VERSION.SDK_INT >= 24) {
                str = getStoragePath(context, true);
            } else {
                str = getPhysicalExternalFilePathAboveM();
            }
        } else {
            str = System.getenv("SECONDARY_STORAGE");
        }
        context.getExternalFilesDir(null);
        L.e("外置sd卡路径" + str);
        return str + "/昌乐县一张图/";
    }

    private static String getStoragePath(Context context, boolean z) {
        StorageManager storageManager = (StorageManager) context.getSystemService("storage");
        try {
            Class<?> cls = Class.forName("android.os.storage.StorageVolume");
            Method method = storageManager.getClass().getMethod("getVolumeList", new Class[0]);
            Method method2 = cls.getMethod("getPath", new Class[0]);
            Method method3 = cls.getMethod("isRemovable", new Class[0]);
            Object invoke = method.invoke(storageManager, new Object[0]);
            int length = Array.getLength(invoke);
            for (int i = 0; i < length; i++) {
                Object obj = Array.get(invoke, i);
                String str = (String) method2.invoke(obj, new Object[0]);
                if (z == ((Boolean) method3.invoke(obj, new Object[0])).booleanValue()) {
                    return str;
                }
            }
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e2) {
            e2.printStackTrace();
            return null;
        } catch (NoSuchMethodException e3) {
            e3.printStackTrace();
            return null;
        } catch (InvocationTargetException e4) {
            e4.printStackTrace();
            return null;
        }
    }

    private static String getPhysicalExternalFilePathAboveM() {
        try {
            Class<?> cls = Class.forName("android.os.Environment$UserEnvironment");
            Method declaredMethod = cls.getDeclaredMethod("getExternalDirs", new Class[0]);
            declaredMethod.setAccessible(true);
            Method declaredMethod2 = Class.forName("android.os.UserHandle").getDeclaredMethod("myUserId", new Class[0]);
            declaredMethod2.setAccessible(true);
            File[] fileArr = (File[]) declaredMethod.invoke(cls.getDeclaredConstructor(Integer.TYPE).newInstance(Integer.valueOf(((Integer) declaredMethod2.invoke(UserHandle.class, new Object[0])).intValue())), new Object[0]);
            for (int i = 0; i < fileArr.length; i++) {
                if (Environment.isExternalStorageRemovable(fileArr[i])) {
                    return fileArr[i].getPath();
                }
            }
            return "";
        } catch (Exception unused) {
            return "";
        }
    }

    public static String getVersionName(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int getVersionCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String dealRateStr(String str, int i) {
        int indexOf;
        if (str.indexOf(".") == -1) {
            return str + ".00";
        }
        String replace = str.replace(str.substring(0, str.indexOf(".") + 1), "");
        if (replace.length() < i) {
            replace = replace + "0";
        }
        return str.substring(0, indexOf) + "." + replace.substring(0, i);
    }
}
